package com.xht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xht.constants.SystemConstants;
import com.xht.domain.ResponseResult;
import com.xht.domain.dto.LinkChangeStatusDto;
import com.xht.domain.entity.Link;
import com.xht.mapper.LinkMapper;
import com.xht.service.LinkService;
import com.xht.utils.BeanCopyUtils;
import com.xht.vo.LinkVo;
import com.xht.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2025-02-23 14:21:29
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult getAllLink() {

        //查询  审核通过 （0：表示审核通过； 1：表示未通过 ； 2：表示未审核）的 友联数据

        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> links = list(queryWrapper);

        //封装 LINKVO
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    /**
     * 模糊 和 分页查询 友联列表
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public ResponseResult linkList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.hasText(name), Link::getName, name);
        queryWrapper.eq(StringUtils.hasText(status), Link::getStatus, status);

        Page<Link> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        List<Link> records = page.getRecords();
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(records, LinkVo.class);
        PageVo pageVo = new PageVo(linkVos, page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    /**
     * 新增友联
     *
     * @param link
     * @return
     */
    @Override
    public ResponseResult addLink(Link link) {
        save(link);
        return ResponseResult.okResult();
    }

    /**
     * 根据id回显友联信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getLinkById(Integer id) {
        //LambdaQueryWrapper<Link> queryWrapper = Wrappers.lambdaQuery(Link.class).eq(Link::getId, id);
        Link link = baseMapper.selectById(id);
        return ResponseResult.okResult(link);
    }

    @Override
    public ResponseResult updateLink(Link link) {
        LambdaUpdateWrapper<Link> updateWrapper = Wrappers.lambdaUpdate(Link.class).eq(Link::getId, link.getId());
        update(link, updateWrapper);
        return ResponseResult.okResult();
    }

    /**
     * 逻辑删除友联
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteLink(Integer id) {
        LambdaUpdateWrapper<Link> updateWrapper = Wrappers.lambdaUpdate(Link.class).eq(Link::getId, id)
                .set(Link::getDelFlag, SystemConstants.LINK_DELETE);
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    /**
     * 改变友联状态
     *
     * @param linkChangeStatusDto
     * @return
     */
    @Override
    public ResponseResult changeLinkStatus(LinkChangeStatusDto linkChangeStatusDto) {
        //linkId
        Long id = linkChangeStatusDto.getId();
        String status = linkChangeStatusDto.getStatus();

        //更新 友联状态
        LambdaUpdateWrapper<Link> updateWrapper = Wrappers.lambdaUpdate(Link.class).eq(Link::getId, id)
                .set(Link::getStatus, status);
        update(updateWrapper);
        return ResponseResult.okResult();
    }

}

